﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace PitLib
{
    public class DataStorage
    {
        #region Fields
        private bool _isLogging;
        private StreamWriter _writer;
        private string _fileName;
        #endregion

        public event Action<string> ExceptionOccured;

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="filename">filename</param>
        public DataStorage(string filename)
        {
            _isLogging = false;
            _fileName = filename;
        }

        /// <summary>
        /// Create a log file 
        /// </summary>
        /// <param name="filename">filename</param>
        /// <returns>Result error check</returns>
        private int createLog(string filename)
        {
            int result = -1;
            try
            {
                _fileName = filename;
                _writer = new StreamWriter(filename, true);
                result = 1;
                return result;
            }
            catch (Exception ex)
            {
                ExceptionOccured(ex.Message);
                return result;
            }

            
        }

        /// <summary>
        /// Write default start messages in logfile
        /// </summary>
        /// <returns>Error check result</returns>
        public int StartLogging()
        {
            createLog(_fileName);
            int result = -1;

            try
            {
                _writer.WriteLine("---Start of Log ---");
                _writer.WriteLine("Time;Distance;Speed;Section;Amperage;Voltage;Watt,Temperature");
                result = 1;
                _isLogging = true;
            }
            catch (Exception ex)
            {
                ExceptionOccured(ex.Message);
                return result;
            }

            return result;
        }

        /// <summary>
        /// Write default start messages in logfile
        /// </summary>
        /// <returns>Error check result</returns>
        public int StartLoggingError()
        {
            createLog(_fileName);
            int result = -1;

            try
            {
                _writer.WriteLine("---Start of error Log ---");
                _writer.WriteLine(DateTime.Now.ToString());
                result = 1;
                _isLogging = true;
            }
            catch (Exception ex)
            {
                ExceptionOccured(ex.Message);
                return result;
            }

            return result;
        }

        /// <summary>
        /// Log values in a logfile
        /// </summary>
        /// <param name="data">string array with data</param>
        /// <returns>error check result</returns>
        public int Log(string[] data)
        {
            int result = -1;
            if (_isLogging)
            {
                try
                {
                    _writer.Write(DateTime.Now.ToString() + ";");
                    foreach (var value in data)
                    {
                        _writer.Write(value + ";");
                    }
                    _writer.WriteLine("");
                    result = 1;
                }
                catch (Exception ex)
                {
                    ExceptionOccured(ex.Message);
                    return result;
                }
            }
            return result;
        }

        /// <summary>
        /// Log values in a logfile
        /// </summary>
        /// <param name="data">string</param>
        /// <returns>error check result</returns>
        public int Log(string message)
        {
            int result = -1;
            if (_isLogging)
            {
                try
                {
                    _writer.Write(DateTime.Now.ToString() + ";" + message + ";");
                    _writer.WriteLine("");
                    result = 1;
                }
                catch (Exception ex)
                {
                    ExceptionOccured(ex.Message);
                    return result;
                }
            }
            return result;
        }

        /// <summary>
        /// Stop the logging, close the stream
        /// </summary>
        /// <returns>error check result</returns>
        public int StopLogging()
        {
            int result = -1;
            if (_isLogging)
            {
                _isLogging = false;
                try
                {
                    _writer.WriteLine("---End of Log ---");
                    _writer.Close();
                    result = 1;
                }
                catch (Exception ex)
                {
                    ExceptionOccured(ex.Message);
                    return result;
                }
            }
            return result;
        }
    }
}